package com.party.core.service.mall.impl;

import com.google.common.base.Strings;
import com.party.common.paging.Page;
import com.party.core.dao.read.mall.MallShoppingCartReadDao;
import com.party.core.dao.write.mall.MallShoppingCartWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.model.mall.MallShoppingCart;
import com.party.core.model.mall.MallShoppingCartOutput;
import com.party.core.service.mall.IMallShoppingCartService;
import com.sun.istack.NotNull;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author: Administrator.
 * @Description: 商城购物车
 * @Date:Created in 2017/8/29 0029 上午 11:50.
 */
@Service
public class MallShoppingCartService implements IMallShoppingCartService {
    @Autowired
    private MallShoppingCartReadDao mallShoppingCartReadDao;
    @Autowired
    private MallShoppingCartWriteDao mallShoppingCartWriteDao;

    /**
     * 商城购物车插入
     *
     * @param mallShoppingCart 商城购物车
     * @return 插入编号
     */
    @Override
    public String insert(MallShoppingCart mallShoppingCart) {
        BaseModel.preInsert(mallShoppingCart);
        boolean result = mallShoppingCartWriteDao.insert(mallShoppingCart);
        if (result) {
            return mallShoppingCart.getId();
        }
        return null;
    }

    /**
     * 更新商城购物车
     *
     * @param mallShoppingCart 商城购物车
     * @return 更新结果（true/false）
     */
    @Override
    public boolean update(MallShoppingCart mallShoppingCart) {
        mallShoppingCart.setUpdateDate(new Date());
        return mallShoppingCartWriteDao.update(mallShoppingCart);
    }

    /**
     * 逻辑删除
     *
     * @param id 实体主键
     * @return 删除结果（true/false）
     */
    @Override
    public boolean deleteLogic(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return mallShoppingCartWriteDao.deleteLogic(id);
    }

    /**
     * 物理删除
     *
     * @param id 实体主键
     * @return 删除结果（true/false）
     */
    @Override
    public boolean delete(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return mallShoppingCartWriteDao.delete(id);
    }

    /**
     * 批量逻辑删除
     *
     * @param ids 主键集合
     * @return 删除结果（true/false）
     */
    @Override
    public boolean batchDeleteLogic(@NotNull Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return mallShoppingCartWriteDao.batchDeleteLogic(ids);
    }

    /**
     * 批量物理删除
     *
     * @param ids 主键集合
     * @return 删除结果
     */
    @Override
    public boolean batchDelete(@NotNull Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return mallShoppingCartWriteDao.batchDelete(ids);
    }

    /**
     * 获取商城商品
     *
     * @param id 主键
     * @return 商城商品
     */
    @Override
    public MallShoppingCart get(String id) {
        return mallShoppingCartReadDao.get(id);
    }

    /**
     * 分页查询
     *
     * @param mallShoppingCart 商城商品
     * @param page             分页信息
     * @return 列表
     */
    @Override
    public List<MallShoppingCart> listPage(MallShoppingCart mallShoppingCart, Page page) {
        return mallShoppingCartReadDao.listPage(mallShoppingCart, page);
    }

    /**
     * 所有商城商品
     *
     * @param mallShoppingCart 商城购物车
     * @return 列表
     */
    @Override
    public List<MallShoppingCart> list(MallShoppingCart mallShoppingCart) {
        return mallShoppingCartReadDao.listPage(mallShoppingCart, null);
    }

    /**
     * @param ids              主键集合
     * @param mallShoppingCart
     * @param page             分页信息
     * @return
     */
    @Override
    public List<MallShoppingCart> batchList(@NotNull Set<String> ids, MallShoppingCart mallShoppingCart, Page page) {
        return null;
    }

    @Override
    public List<MallShoppingCart> findByMemberId(String memberId) {
        MallShoppingCart cart = new MallShoppingCart();
        cart.setMemberId(memberId);
        return mallShoppingCartReadDao.listPage(cart, null);
    }

    @Override
    public MallShoppingCart getUnique(MallShoppingCart cart) {
        return mallShoppingCartReadDao.getUnique(cart);
    }

    @Override
    public List<MallShoppingCartOutput> newListPage(Map<String, Object> params, Page page) {
        return mallShoppingCartReadDao.newListPage(params, page);
    }

    @Override
    public List<MallShoppingCart> findByProductId(String productId) {
        MallShoppingCart cart = new MallShoppingCart();
        cart.setProductId(productId);
        return mallShoppingCartReadDao.listPage(cart, null);
    }

    @Override
    public void deleteByProductId(String productId) {
        mallShoppingCartWriteDao.deleteByProductId(productId);
    }
}
